/**
 * 大厅界面
 * @author none
 */
class MainSceneModule extends how.module.SceneModule
{
    public static response: any = 
    {
        updateHeader: "updateHeader",//修改玩家信息,
        updateMoney: CMDConfig.GET_UPDATE_MONEY,//更新金币
        changeServer: "doChangeServer",
    }

    public static request: any = 
    {
        onSetting: "onSetting",
        onHeader: "onHeader",
        onGameBtn: "onGameBtn",
    }

    public start(): void
    {
        how.SoundManager.playMusic("hall_bg_mp3");
        how.ComponentUtils.init(true, "public.AlertSkin", "public.DialogSkin", "public.BannerSkin", "MainSceneViewNoticeSkin", "public.LoaddingSkin",
            "GameSceneAnnouncement", "SettingWindowSkin");//初始化通用组件
        this.showKindList();
    }

    private showKindList()
    {
        var gameList: Array<GameData> = AppData.getInstance().gameList;
        var itemDatas: any = [];
        for(var game of gameList)
        {
            var rooms: Array<RoomData> = AppData.getInstance().roomList.filter(value => value.kindID == game.id);
            var itemData = {
                gameName: rooms[0].name,
                kindID: rooms[0].kindID,
            }
            itemDatas.push(itemData);
        }
        this.callUI("showKindList", itemDatas);
    }

    //按下玩家头像
    public onHeader(): void 
    {
        this.moduleManager.initModule(AvaterWindowModule, AvaterWindowView, AvaterWindowData);
    }

    //按下设置按钮
    public onSetting(): void 
    {
        how.SettingWindow.show();
    }

    //收到修改头像
    public updateHeader(data: any): void 
    {
        this.callUI("updateHeader");
    }

    //更新金币
    private updateMoney(data: any) 
    {
        this.callUI("setMoney", data.score);
    }

    //点击某个游戏
    public onGameBtn(data: any): void 
    {
        var gameData = base.Utils.getItem(AppData.getInstance().gameList, "id", data.id);
        if (gameData)
        {
            AppData.getInstance().currentGameIndex = gameData.id;
            AppData.getInstance().currentGame = gameData;

            var allRoomList = AppData.getInstance().roomList;
            var roomList = [];
            var roomCollection: eui.ArrayCollection = null;
            if (allRoomList && allRoomList.length > 0) 
            {
                for(var i = 0; i < allRoomList.length; ++i)
                {
                    var roomData: RoomData = allRoomList[i];
                    if(roomData.kindID == gameData.id)
                    {
                        roomList.push(roomData);
                    }
                }
                var roomCollection = new eui.ArrayCollection(roomList);
            }
            let roomlistWindow = this.moduleManager.initModule(RoomListWindowModule, RoomListWindowView) as RoomListWindowModule;
            roomlistWindow.updateData(roomCollection);

        }
    }

    public changeServer():void
    {
        this.communicate(base.ChangeServerGlobalModule.response.changeServer, AppData.getInstance().currentRoom, false, (flag) => {
        if(flag)
        {
            var kindClassString: string = AppData.getInstance().kindClassMap[AppData.getInstance().currentGame.id];
            if(kindClassString)
            {
                 if(kindClassString.indexOf(",") == -1)
                {
                    var className = egret.getDefinitionByName(kindClassString);
                    D.App.enterGame(new className());
                }
                else
                {
                    // 兼容老版本代码
                    var kindClass = kindClassString.split(",");
                    var module = egret.getDefinitionByName(kindClass[0].trim());
                    var view = egret.getDefinitionByName(kindClass[1].trim());
                    var data = egret.getDefinitionByName(kindClass[2].trim());

                    this.moduleManager.initGlobalModule(base.GameSceneGlobalModule, data);//游戏数据模块
                    this.moduleManager.initModule(module, view, data);
                }
            }
            else
            {
                console.log(">>> 该KindID还未配置对应的场景文件 kindID:" + AppData.getInstance().currentGame.id);
                how.Alert.show(">>> 该KindID还未配置对应的场景文件 kindID:" + AppData.getInstance().currentGame.id);
            }
        }
        else
        {
            how.Alert.show(LanguageConfig.hall_error_100);
        }
        });
    }

    private onUpdataMoney(): void 
    {
        this.callUI("updataMoney");
    }
}

window["MainSceneModule"] || (window["MainSceneModule"] = MainSceneModule);
